package com.as3.timbot.iso
{
	public class Isometric
	{
		private static var maxx:Number;
 		private static var maxz:Number;
		private static var theta:Number;
		private static var alpha:Number;
		private static var sinTheta:Number;
		private static var cosTheta:Number;
		private static var sinAlpha:Number;
		private static var cosAlpha:Number;
		private static var leeway:Number;
		
		public function Isometric(x:Number = 0, z:Number = 0) 
		{
			maxx = x;
			maxz = z;
			theta = 30;
			alpha = 45;
			theta *= Math.PI/180;
			alpha *= Math.PI/180;
			sinTheta = Math.sin(theta);
			cosTheta = Math.cos(theta);
			sinAlpha = Math.sin(alpha);
			cosAlpha = Math.cos(alpha);
			leeway = 5;
		}
		public function mapToScreen(xpp:Number, ypp:Number, zpp:Number):Array 
		{
			var yp:Number = ypp;
			var xp:Number = xpp*cosAlpha+zpp*sinAlpha;
			var zp:Number = zpp*cosAlpha-xpp*sinAlpha;
			var x:Number = xp;
			var y:Number = yp*cosTheta-zp*sinTheta;
			return [x, y];
		}
		public function mapToIsoWorld(screenX:Number, screenY:Number):Array 
		{
			var z:Number = (screenX/cosAlpha-screenY/(sinAlpha*sinTheta))*(1/(cosAlpha/sinAlpha+sinAlpha/cosAlpha));
			var x:Number = (1/cosAlpha)*(screenX-z*sinAlpha);
			return [x, z];
		}
		//the following two functions can probably go away in AS3...........t.w.
		public function setLeeway(value:Number):void
		{
			leeway = value;
		}
		public function calculateDepth(x:Number, y:Number, z:Number):Number 
		{
			var x:Number = Math.abs(x)*leeway;
			var y:Number = Math.abs(y);
			var z:Number = Math.abs(z)*leeway;
			var a:Number = maxx;
			var b:Number = maxz;
			var floor:Number = a*(b-1)+x;
			var depth:Number = a*(z-1)+x+floor*y;
			return depth;
		}
	}
}